#include "StdAfx.h"
#include "GLog.h"
#include "GStringUtil.h"

#include <conio.h>
#include <stdio.h>

char g_traceList[] = "Sanguosha,GObject,XmlWrapper";

const static int GS_MAX_FILENAME = 64;

bool GLog::_enabled = true;
char** GLog::_traceList = 0;
int GLog::_traceNum = 0;

GLog::GLog()
{
}


GLog::~GLog()
{
}

void GLog::initialize(){
	_enabled = true;
	if( !_enabled )
		return;

	_traceList = GStringUtil::split( g_traceList, ',', _traceNum );
	for( int i = 0; i < _traceNum; ++i )
		GStringUtil::toLowerCase( _traceList[ i ] );

	AllocConsole();
	freopen( "CONOUT$", "w", stdout );
}

void GLog::finalize(){
	if( !_enabled )
		return;

	FreeConsole();

	for( int i = 0; i < _traceNum; ++i )
		delete [] _traceList[ i ];
	delete [] _traceList;
}

bool GLog::shouldLogFile( char * fileName ){
	if( !_enabled )
		return false;

	GStringUtil::toLowerCase( fileName );
	bool log = false;
	for( int i = 0; i < _traceNum; ++i )
		if( strcmp( fileName, _traceList[ i ] ) == 0 ){
			log = true;
			break;
		}
	return log;
}

char* GLog::getTraceLevel( const char * fullPath ){
	char* traceLevel = new char[ GS_MAX_FILENAME ];
	GStringUtil::analyseFilePath( fullPath, 0, traceLevel, 0 );
	return traceLevel;
}